package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.lljjcoder.citypickerview.widget.CityPickerView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.net.URLEncoder;
import java.util.Calendar;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Modify_the_Dept extends AppCompatActivity {

    private EditText modify_the_DeptName;
    private RadioButton modify_the_Deptsex1;
    private RadioButton modify_the_Deptsex2;
    private EditText modify_the_DeptAge;
    private EditText modify_the_Deptcarid;
    private EditText modify_the_DeptPhone;
    private EditText modify_the_DeptEmail;
    private TextView modify_the_DeptWorkerLocal;
    private TextView modify_the_DeptbackTime;
    private Button modify_the_Dept_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_the_dept);
        initid();
        initview();
        modify_the_Dept_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tijiao();
            }
        });
        modify_the_DeptbackTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDatePickerDialog(modify_the_DeptbackTime);
            }
        });

        modify_the_DeptWorkerLocal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CityPickerView cityPickerView = new CityPickerView(Modify_the_Dept.this);
                cityPickerView.setOnCityItemClickListener(new CityPickerView.OnCityItemClickListener() {
                    @Override
                    public void onSelected(String... citySelected) {
                        //省份
                        String province = citySelected[0];
                        //城市
                        String city = citySelected[1];
                        //区县
                        String district = citySelected[2];
                        //邮编
                        String code = citySelected[3];
                        //控件自己写两个edittext或者Xedittext用来显示文本代替下面两个个控件ID
                        //address_new_address用来展示地址
                        //address_new_zipcode用来展示邮编
                        modify_the_DeptWorkerLocal.setText(province+"-"+ city+"-"+ district);
                    }
                });
                cityPickerView.show();

            }
        });

    }

    private void initid(){

        modify_the_DeptName = (EditText) findViewById(R.id.modify_the_DeptName);
        modify_the_Deptsex1 = (RadioButton) findViewById(R.id.modify_the_Deptsex1);
        modify_the_Deptsex2 = (RadioButton) findViewById(R.id.modify_the_Deptsex2);
        modify_the_DeptAge = (EditText) findViewById(R.id.modify_the_DeptAge);
        modify_the_Deptcarid = (EditText) findViewById(R.id.modify_the_Deptcarid);
        modify_the_DeptPhone = (EditText) findViewById(R.id.modify_the_DeptPhone);
        modify_the_DeptEmail = (EditText) findViewById(R.id.modify_the_DeptEmail);
        modify_the_DeptWorkerLocal = (TextView) findViewById(R.id.modify_the_DeptWorkerLocal);
        modify_the_DeptbackTime = (TextView) findViewById(R.id.modify_the_DeptbackTime);

        modify_the_Dept_btn = (Button) findViewById(R.id.modify_the_Dept_btn);
    }

    private void initview(){
        //修改名字
        modify_the_DeptName.setText(Sto.dept.getWorkerName());
        //修改性别
        if (Sto.dept.getWorkerSex().equals("男")){
            modify_the_Deptsex1.setChecked(true);
        }else {
            modify_the_Deptsex2.setChecked(true);
        }

        //修改年龄
        modify_the_DeptAge.setText(Sto.dept.getWorkerAge()+"");
        //修改身份证
        modify_the_Deptcarid.setText(Sto.dept.getWorkerCarid());
        //修改手机号
        modify_the_DeptPhone.setText(Sto.dept.getWorkerPhone());
        //修改邮箱
        modify_the_DeptEmail.setText(Sto.dept.getWorkerEmail());
        //修改地址
        modify_the_DeptWorkerLocal.setText(Sto.dept.getWorkerLocal());
        //修改入职时间
        String str=Sto.dept.getWorkerbackTime();

        str=str.substring(0,10);
        modify_the_DeptbackTime.setText(str);
    }

    //弹出选择日期的对话框
    private void showDatePickerDialog(final TextView textView){
        Calendar c = Calendar.getInstance();
        new DatePickerDialog(Modify_the_Dept.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                textView.setText(i+"-"+(i1+1)+"-"+i2);
            }
        },c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show();
    }

    private void tijiao(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("WorkerId",Sto.dept.getWorkerId());
                    jsonObject.put("WorkerName",modify_the_DeptName.getText().toString());
                    jsonObject.put("WorkerAge",Integer.parseInt(modify_the_DeptAge.getText().toString()));
                    jsonObject.put("WorkerbackTime",modify_the_DeptbackTime.getText().toString());
                    jsonObject.put("WorkerCarid",modify_the_Deptcarid.getText().toString());
                    jsonObject.put("WorkerEmail",modify_the_DeptEmail.getText().toString());
                    jsonObject.put("WorkerPhone",modify_the_DeptPhone.getText().toString());
                    jsonObject.put("WorkerLocal",modify_the_DeptWorkerLocal.getText().toString());
                    jsonObject.put("PositionId",Sto.dept.getPositionId());
                    String sex;
                    if (modify_the_Deptsex1.isChecked()){
                        sex = "男";
                    }else {
                        sex = "女";
                    }
                    jsonObject.put("WorkerSex",sex);

                    JSONArray jsonArray = new JSONArray();
                    jsonArray.put(0,jsonObject);
                    JSONObject jsonObjects = new JSONObject();
                    jsonObjects.put("token",Sto.dept.getToken());
                    jsonObjects.put("InfoJSON",jsonArray);
                    jsonObjects.put("ApplicationType",3);
                    jsonObjects.put("Status",2);
                    Log.d("修改数据",jsonObjects.getString("InfoJSON"));
                    Log.d("数据",jsonObjects.toString());
                    MediaType mediaType = MediaType.parse("application/json");
                    RequestBody requestBody = RequestBody.create(mediaType,"");
                    String result = URLEncoder.encode(jsonObjects.getString("InfoJSON"), "utf-8");
                    String urls = Sto.NetIP+"/api/TheApplicationForm/InsertApplicationForm?token="+Sto.dept.getToken()+"&InfoJSON="+result+"&ApplicationType=3&Status=2";
                    Log.d("url",urls);
                    Request request1 = new Request.Builder()
                            .url(urls)
                            .post(requestBody)
                            .build();
                    Response response = client.newCall(request1).execute();
                    String results = response.body().string();
                    JSONObject jsonObject1 = new JSONObject(results);
                    String code = jsonObject1.getString("code");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (code.equals("200")){
                                Toast.makeText(Modify_the_Dept.this,"申请成功",Toast.LENGTH_SHORT).show();
                                finish();
                            }else {
                                Toast.makeText(Modify_the_Dept.this,"申请失败",Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}